const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
    // 读取图片文件
    fs.readFile('./node.jpg', (err, data) => {
        if (err) {
            res.writeHead(404);
            res.end('图片未找到');
            return;
        }

        // 将图片转换为base64
        const base64Img = data.toString('base64');

        // 创建HTML响应
        const html = `
            <!DOCTYPE html>
            <html>
            <body>
                <img src="data:image/jpeg;base64,${base64Img}" alt="Node.js Book">
            </body>
            </html>
        `;

        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(html);
    });
});

server.listen(8081, () => {
    console.log('服务器运行在 http://localhost:8081/');
});